home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Entertainment / MacMud / Sockets / socket.internal.h < prev    next >
Encoding:
C/C++ Source or Header  |  1990-03-21  |  3.8 KB  |  108 lines  |  [TEXT/MPS ]

  1. /*
  2.  *    A more or less BSD compatable socket library for MacTCP
  3.  *    
  4.  *    Summer 1989, Tom Milligan, University of Toronto Computing Services
  5.  */
  6.  
  7.      
  8. #define NUM_SOCKETS        32        /* Number of sockets.  Should never exceed 32 */
  9.  
  10. #define STREAM_BUFFER_SIZE     20000    /* memory for MacTCP streams */
  11.  
  12. #define UDP_MAX_MSG        65507    /* MacTCP max legal udp message */
  13. #define TCP_MAX_MSG        65535    /* MacTCP max legal tcp message */
  14.  
  15. #define TCP_MAX_WRITES    16        /* socket (not TCP) max writes in progress */
  16.  
  17. /*
  18.  *    In use and shutdown status.
  19.  */
  20. #define    SOCK_STATUS_USED        0x1        /* Unused socket table entry */
  21. #define    SOCK_STATUS_NOREAD        0x2        /* No more reading allowed from socket */
  22. #define    SOCK_STATUS_NOWRITE        0x4        /* No more writing allowed to socket */
  23.  
  24. /*
  25.  *    Socket connection states.
  26.  */
  27. #define SOCK_STATE_NO_STREAM    0    /* socket doesn't have a MAcTCP stream yet */
  28. #define    SOCK_STATE_UNCONNECTED    1    /* Socket is unconnected. */
  29. #define    SOCK_STATE_LISTENING    2    /* Socket is listening for connection. */
  30. #define    SOCK_STATE_LIS_CON        3    /* Socket is in transition from listen to connected. */
  31. #define    SOCK_STATE_CONNECTING    4    /* Socket is initiating a connection. */
  32. #define    SOCK_STATE_CONNECTED    5    /* Socket is connected. */
  33.  
  34. typedef union mactcp_pb    
  35. {
  36.     TCPiopb                tcp;
  37.     UDPiopb                udp;
  38.     struct IPParamBlock    ip;
  39. } mactcp_pb;
  40.  
  41. typedef struct async_pb 
  42. {
  43.     mactcp_pb             pb;            /* MacTCP parameter block */
  44.     struct SocketRecord    *sp;        /* ptr back to socket for completion routines */
  45. } async_pb;
  46.  
  47. typedef struct write_pb 
  48. {
  49.     mactcp_pb             pb;            /* MacTCP parameter block */
  50.     struct SocketRecord    *sp;        /* ptr back to socket for completion routines */
  51.     wdsEntry            wds[2];        /* single entry MacTCP wds structure */
  52. } write_pb;
  53.  
  54. typedef struct SocketRecord 
  55. {
  56.     mactcp_pb    pb;                    /* parameter block for synchronous operations */
  57.     async_pb     apb;                /* parameter block for read, listen and connect */
  58.     write_pb    wpb[TCP_MAX_WRITES];/* parameter blocks for write */
  59.     short        nextwpb;            /* index of next wpb to use */
  60.     byte                status;        /* Is file descriptor in use */
  61.     int                    fd;            /* fd number */
  62.     struct sockaddr_in    sa;            /* My address. */
  63.     struct sockaddr_in    peer;        /* Her address. */
  64.     short                protocol;    /* Protocol (e.g. TCP, UDP) */
  65.     Boolean                nonblocking;/* socket set for non-blocking I/O. */
  66.     byte                sstate;        /* socket's connection state. */
  67.     unsigned long        dataavail;    /* Amount of data available on connection. */
  68.     int                    asyncerr;    /* Last async error to arrive.  zero if none. */
  69.     /* stdio stuff */
  70.     char                *outbuf;    /* Ptr to array to buffer output */
  71.     int                    outbufcount;/* # of characters in outbuf */
  72.     Ptr                    outbufptr;    /* Pointer into outbuf */
  73.     char                *inbuf;        /* Ptr to array to buffer input */
  74.     int                    inbufcount;    /* # of characters in inbuf */
  75.     Ptr                    inbufptr;    /* Pointer into inbuf */
  76.     Boolean                ioerr;        /* Holds error status for stdio calls */
  77.     Boolean                ioeof;        /* EOF was detected on stream */
  78. } SocketRecord, *SocketPtr;
  79.  
  80.  
  81. /*-------------------------------------------------------------------------*/
  82. #define        sock_set(f,s)            f |= (1 << s)
  83. #define        sock_is_set(f,s)        (((f) & (1 << s)) != 0)
  84.  
  85. #define SOCK_MIN_PTR    (Ptr)0x1400        /* Minimum reasonable pointer */
  86. #define        goodptr(p)                ((p) > SOCK_MIN_PTR)
  87. #define        is_used(p)                (goodptr(p) && (p)->status & SOCK_STATUS_USED)
  88. #define        is_stdio(p)                (is_used(p) && (p)->inbuf != NULL)
  89. #define        sock_good_fd(s)            ((0 <= s && s < NUM_SOCKETS) && is_used (&sockets[s]))
  90. #define        sock_nowrite(p)            ((p)->status & SOCK_STATUS_NOWRITE)
  91. #define        sock_noread(p)            ((p)->status & SOCK_STATUS_NOREAD)
  92.  
  93. #define     TVTOTICK(tvsec,tvusec)    ( ((tvsec)*60) + ((tvusec)/16666) )
  94. #define        min(a,b)                ( (a) < (b) ? (a) : (b))
  95. #define        max(a,b)                ( (a) > (b) ? (a) : (b))
  96.  
  97.  
  98. /*
  99.  *    Global storage
  100.  */
  101. #ifdef SOCKET
  102.     SocketPtr sockets = NULL;    /* The socket table. */
  103.     ProcPtr spinroutine = NULL;    /* The spin routine. */
  104. #else
  105.     extern SocketPtr sockets;
  106.     extern ProcPtr spinroutine;
  107. #endif
  108.